package com.byxy.xyqas.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.byxy.xyqas.dao.ReplyDao;
import com.byxy.xyqas.dao.UserDao;
import com.byxy.xyqas.entity.Reply;
import com.byxy.xyqas.entity.User;
import com.byxy.xyqas.service.ReplyService;
import com.byxy.xyqas.vo.ReplyVO;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class ReplyServiceImpl implements ReplyService {
	@Autowired
	private ReplyDao replyDao;

	@Autowired
	private UserDao userDao;

	@Override
	public List<ReplyVO> findAll() {
		List<ReplyVO> replyVOs = new ArrayList<>();
		List<Reply> replies = replyDao.findAll();
		for (Reply reply : replies) {
			ReplyVO replyVO = new ReplyVO();
			BeanUtils.copyProperties(reply, replyVO);
			User u = userDao.getOne(reply.getUserId());
			replyVO.setUserName(u.getUserName());
			replyVO.setUserNick(u.getUserNick());
			replyVOs.add(replyVO);
		}
		return replyVOs;
	}

	@Override
	public ReplyVO get(Integer replyId) {
		return null;
	}

	@Override
	public void delete(Integer tipId) {
		replyDao.deleteById(tipId);
	}

	@Override
	public void add(ReplyVO reply) {
		Reply r = new Reply();
		BeanUtils.copyProperties(reply, r);
		replyDao.save(r);
	}

}
